package com.itany.controller;

import com.github.pagehelper.PageInfo;
import com.itany.constant.StatusConst;
import com.itany.mapper.SupplierMapper;
import com.itany.pojo.Supplier;
import com.itany.pojo.SupplierExample;
import com.itany.service.SupplierService;
import com.itany.vo.ActionResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/supplier")
public class SupplierController {
    @Autowired
    private SupplierMapper supplierMapper;

    @Autowired
    private SupplierService supplierService;

    @RequestMapping("findAll")
    public ActionResult findAll() {
        ActionResult result = new ActionResult();
        List<Supplier> suppliers = supplierMapper.selectByExample(new SupplierExample());
        result.setData(suppliers);
        return result;
    }

    @RequestMapping("/platformFindAll")
    public Map<String, Object> findAll(
            @RequestParam(defaultValue = "1") Integer page,
            @RequestParam(defaultValue = "10") Integer rows,
            String name) {
        Map<String, Object> map = new HashMap<String, Object>();
        PageInfo<Supplier> info = supplierService.findAllExample(page, rows, name);
        map.put("total", info.getTotal());
        map.put("rows", info.getList());
        return map;
    }

    @RequestMapping("/add")
    public ActionResult add(Supplier supplier) {
        ActionResult result = new ActionResult();
        supplierService.add(supplier);
        return result;
    }


    @RequestMapping("/update")
    public ActionResult update(Supplier supplier) {
        ActionResult result = new ActionResult();

        try {
            supplierService.update(supplier);
        } catch (Exception e) {
            result.setMsg(e.getMessage());
            result.setStatus(StatusConst.FAIL_RESPONSE);
        }
        return result;
    }

    //    @RequestMapping("/findOne")
    public ActionResult findOne(Integer id) {
        ActionResult result = new ActionResult();
        return result;
    }

}
